<?php
session_start();
if (isset($_POST['phone']) && isset($_SESSION['openid'])){
    include '../c/config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    $mysqli->query("SET NAMES utf8");
    $phone=addslashes(trim($_POST['phone']));
    $gps=json_encode(array('lat'=>addslashes(trim($_POST['lat'])),'lon'=>addslashes(trim($_POST['lon']))));
    $ip=get_real_ip();
    $queryName=$mysqli->query("select id,name from ud where phone='".$phone."' limit 0, 1");
    if($queryName->num_rows>0){
        $getU=$queryName->fetch_assoc();
        $mysqli->query("insert into uinfo(ip,openid,phone,gps,createtime) values('".$ip."','".$_SESSION['openid']."','".$phone."','".$gps."','".date("Y-m-d H:i:s")."') ");
        echo json_encode(array('s'=>'y','n'=>$getU['name']));
        exit;
    }else{
        echo json_encode(array('s'=>'n','e'=>'name'));
        exit;
    }

    
   
}
function get_real_ip()
{
    $ip=FALSE;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
        
        $ip = $_SERVER["HTTP_CLIENT_IP"];
        
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        
        if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
        
        for ($i = 0; $i < count($ips); $i++) {
            
            if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
                
                $ip = $ips[$i];
                
                break;
                
            }
            
        }  
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    
}
?>
